import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import './plugins/element'
import Cookies from 'js-cookie'

Vue.config.productionTip = false
import http from './http.js'
Vue.prototype.$http = http

import qiniu from './plugins/qiniu'
Vue.use(qiniu)

import {adminRouters} from './router/adminRouters'; //管理员路由表
import {superAdminRouters} from './router/superAdminRouters'; //超级管理员用户路由表

Vue.mixin({
	methods: {
		getAuthHeaders() {
			return {
				Authorization: `Bearer ${sessionStorage.token || ''}`
			}
		}
	}
})

new Vue({
	el: '#app',
	router,
	store,
	qiniu,
	render: h => h(App),
	created() {
		//下面这段代码在实际开发中应该封装起来，因为在登陆的时候也会用到
		//登陆成功则在cookie中设置对应的token值，然后执行类似下面的代码
		//注意：这里的this.$router.addRoutes是vue-router2.2版本后才有的方法
		if (Cookies.get('user_token')&&this.$store.state.user) {
			switch (this.$store.state.user.role) {
				case 'admin':
					this.$router.addRoutes(adminRouters);
					break
				case 'super_admin':
					this.$router.addRoutes(adminRouters);
					this.$router.addRoutes(superAdminRouters);
					break
				default:
					break;
			}
			this.getAuthHeaders()
		} else {
			this.$router.replace('/login')
		}
	},
})
